package com.innovation.chengzhangriji.ui;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.innovation.chengzhangriji.Config;
import com.innovation.chengzhangriji.R;
import com.innovation.chengzhangriji.ui.widget.HeadPhotosAdapter;
import com.innovation.chengzhangriji.ui.widget.HeadPictureViewHolder;
import com.umeng.analytics.MobclickAgent;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;

public class HeadPhotoActivity extends BaseActivity implements HeadPictureViewHolder.OpenPictureListener {

    HeadPhotosAdapter mAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_head_photo);

        RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recycler_view_picture);
        RecyclerView.LayoutManager layoutManager = new GridLayoutManager(this, 4);
        recyclerView.setLayoutManager(layoutManager);
        mAdapter = new HeadPhotosAdapter(this);
        mAdapter.setOpenPictureListener(this);
        recyclerView.setAdapter(mAdapter);

        LoadImageTask a = new LoadImageTask();
        a.execute();
    }

    @Override
    protected void onPageStart() {
        MobclickAgent.onPageStart(Config.UMENG_PAGE_HEAD_PHOTO);
    }

    @Override
    protected void onPageEnd() {
        MobclickAgent.onPageEnd(Config.UMENG_PAGE_HEAD_PHOTO);
    }

    private void queryAllImage() {
        String[] projection = new String[]{MediaStore.Images.Media.DATA};
        Uri images = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
        Cursor cursor = getContentResolver().query(images,
                projection,
                null,
                null,
                MediaStore.Images.Media.DATE_ADDED + " DESC"
        );
        if (cursor.moveToFirst()) {
            String data;
            int dataColumn = cursor.getColumnIndex(MediaStore.Images.Media.DATA);
            do {
                data = cursor.getString(dataColumn);
                /*arrayList.add(data);*/
                mAdapter.addImageUrl(data);
            } while (cursor.moveToNext());
        }
        cursor.close();
    }

    public static final int CROP_INTENT = 0;
    private File cropImage;

    @Override
    public void openPicture(int position) {

        // 裁剪
        cropImage = HeadCameraActivity.getOutputMediaFile(HeadCameraActivity.MEDIA_TYPE_IMAGE);
        Intent intent = new Intent();
        intent.setAction("com.android.camera.action.CROP");
        intent.setDataAndType(Uri.fromFile(new File(mAdapter.getImageUrl(position))), "image/*");   // mUri是已经选择的图片Uri
        intent.putExtra("crop", "true");
        intent.putExtra("aspectX", 1);    // 裁剪框比例
        intent.putExtra("aspectY", 1);
        intent.putExtra("outputX", 120);  // 输出图片大小
        intent.putExtra("outputY", 120);
        intent.putExtra("return-data", false);
        intent.putExtra("scale", true);
        intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(cropImage));
        startActivityForResult(intent, CROP_INTENT);
    }

    private class LoadImageTask extends AsyncTask<Void, Void, Void> {

        @Override
        protected Void doInBackground(Void... params) {
            queryAllImage();
            return null;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(resultCode == RESULT_OK && requestCode == CROP_INTENT) {
            Intent resultIntent = new Intent();
            resultIntent.putExtra(Config.ACTION_EXTERNAL_RESULT_PHOTO, cropImage.getAbsolutePath());
            setResult(RESULT_OK, resultIntent);
        }
        finish();
    }
}
